package com.zzyl.nursing.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component("checkStatusTask")
public class CheckStatusTask {
    @Autowired
    private IReservationService reservationService;

    /**
     * 检查预定状态，并根据当前时间更新状态。
     * 如果预定时间早于当前时间，将状态更新为3（表示已过期）。
     * 如果预定时间晚于当前时间，保持状态为0（表示未开始）。
     */
    public void checkStatus() {
//        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Reservation::getStatus,0);
//        List<Reservation> list = reservationService.list(queryWrapper);
//        for (Reservation reservation : list) {
//            LocalDateTime time = reservation.getTime();
//            if(time.isBefore(LocalDateTime.now())) {
//                reservation.setStatus(3);
//                reservationService.updateById(reservation);
//            }
//            if(time.isAfter(LocalDateTime.now())){
//                reservation.setStatus(0);
//                reservationService.updateById(reservation);
//            }
//        }
        LambdaUpdateWrapper<Reservation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.lt(Reservation::getTime, LocalDateTime.now())
                .eq(Reservation::getStatus, 0)
                .set(Reservation::getStatus, 3);
        reservationService.update(updateWrapper);
    }

}
